/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rapidp2p.backend;


import java.net.*;
import java.io.*;
import rapidp2p.backend.RequestHandler_1;
/**
 *
 * @author LF
 */
public class ClientWorker implements Runnable{
    private Socket client;
    private String filename;
    private String myFile = null;
    
    
    ClientWorker(Socket client, String filename){
        this.client = client;
        this.filename = filename;//right now not doing anything with file name,hard-coded first
    }
    
//    myFile = RequestHandler_1.getfilePath();
//    myFile = myFile.replace('/', File.separatorChar);
//    myFile = myFile.replace('\\', File.separatorChar); // format filename
    
     public void run() {
        System.out.println("Got inside RUN()\n");
        BufferedOutputStream outToClient = null;
        //FileInputStream fis = null;
        try {
            outToClient = new BufferedOutputStream(client.getOutputStream());
        } catch (IOException e) {
            System.out.println(" Out failed\n");
            System.exit(-1);
        }
        System.out.println("outToClient = " + outToClient + "\n");
        if (outToClient != null) {
            //File myFile = new File("C:\\cheat.pdf");"C:\\cheat.pdf");
            
            File myFile = new File("C:\\cheat.pdf");
            System.out.println("File taken\n");
            byte[] mybytearray = new byte[(int) myFile.length()];

            FileInputStream fis = null;

            try {
                fis = new FileInputStream(myFile);
            } catch (FileNotFoundException ex) {
                System.out.println("File not found\n");

            }
            BufferedInputStream bis = new BufferedInputStream(fis);

            try {
                bis.read(mybytearray, 0, mybytearray.length);
                outToClient.write(mybytearray, 0, mybytearray.length);
                outToClient.flush();
                outToClient.close();
                client.close();

                // File sent, exit the main method
                return;
            } catch (IOException ex) {
                // Do exception handling
            }
        }
    }
}
